# main.fy
# This file gets run directly from bin/fancy.
# It handles any given built-in ARGV options.
# If any .fy source filename is passed in via ARGV, it is loaded and
# executed, otherwise ifancy, Fancy's REPL, gets loaded.

if: (ARGV size == 1) then: {
  ARGV for_options: ["-v", "--version"] do: {
    "Fancy #{Fancy VERSION}" println
    "(C) 2010-2014 Christopher Bertels <chris@fancy-lang.org>" println
    System exit
  }

  ARGV for_options: ["--help", "-h"] do: {
    ["Usage: fancy [options] [programfile] [arguments]",
     "  --help                   Print this output",
     "  -h                       Print this output",
     "  --version                Print Fancy's version number",
     "  -v                       Print Fancy's version number",
     "  -I [directory]           Add directory to Fancy's LOAD_PATH",
     "  -e 'command'             One line of Fancy code that gets evaluated immediately",
     "  -c [filenames]           Compile given files to Rubinius bytecode",
     "  -cv [filenames]          Compile given files to Rubinius bytecode verbosely (outputting the generated bytecode)",
     "  -debug                   Start the Rubinius Debugger with Fancy",
     "",
     "Fancy package management:",
     "  install   [packagename]  Install a Fancy package with a given name to $FANCYPACK_DIR",
     "  install   --deps         Install dependencies specified in .fancypack file (expected in current directory)",
     "  uninstall [packagename]  Uninstall a Fancy package with a given name from $FANCYPACK_DIR",
     "  list-packages            List all installed packages"] println
    System exit # quit when running --help
  }
}

ARGV for_option: "-I" do: |path| {
  Fancy CodeLoader push_loadpath: path
}

ARGV for_option: "-e" do: |eval_string| {
  eval_string eval
  System exit # quit when running with -e
}

ARGV for_option: "-c" do: {
  if: (ARGV index: "-c") then: |idx| {
    ARGV[[idx + 1, -1]] . tap: |filenames| {
      total = filenames size
      max_width = total decimals size
      start_time = Time now
      duration = Time duration: {
        filenames each_with_index: |filename idx| {
          *stdout* printf("[%#{max_width}i / %i] Compiling %s\n", idx + 1, total, filename)
          Fancy Compiler compile_file: filename to: nil line: 1 print: false
        }
      }
      files = match filenames size {
        case 1 -> "file"
        case _ -> "files"
      }
      "Compiled #{filenames size} #{files} in #{duration} seconds." println
    }
  }
  System exit
}

ARGV for_option: "-cv" do: {
  if: (ARGV index: "-cv") then: |idx| {
    ARGV[[idx + 1, -1]] each: |filename| {
      "Compiling " ++ filename println
      Fancy Compiler compile_file: filename to: nil line: 1 print: true
    }
  }
  System exit
}

ARGV for_option: "-debug" do: {
  require: "rbx/debugger"
}

ARGV for_option: "install" do: |package_name| {
  match package_name {
    case "--deps" -> Fancy Package install_dependencies
    case _ -> Fancy Package install: package_name
  }
  System exit
}

ARGV for_option: "uninstall" do: |package_name| {
  Fancy Package uninstall: package_name
  System exit
}

ARGV for_option: "list-packages" do: {
  Fancy Package list_packages
  System exit
}

# push package install dir to load_path
Fancy Package add_to_loadpath

# Load a source file, if any given:
if: (ARGV first) then: |file| {
  try {
    Fancy CodeLoader load_compiled_file: file
  } catch Fancy Parser ParseError => e {
    e message println
  }
}

if: (ARGV empty?) then: {
  require: "../bin/ifancy"
}
